/*
 * (C) Copyright 2013 George Daswani (georgedaswani@gmail.com) and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *    George Daswani (georgedaswani@gmail.com)
 */

package am.api.wrapper;

import org.apache.commons.lang.builder.ToStringBuilder;

import am.api.AMConnection;
import am.api.model.AMDate;
import am.api.model.AMString;
import am.api.util.NLS;

public class AMConnectionWrapper implements AMConnection {

	private AMConnection connection;

	private boolean closed;

	private boolean enlisted;

	public AMConnectionWrapper(AMConnection connection, boolean enlisted) {
		this.enlisted = enlisted;
		this.connection = connection;
	}

	public int clearLastError() {
		return connection.clearLastError();
	}

	public String toString() {
		return new ToStringBuilder(this).append("connection", connection)
				.append("closed", closed).append("enlisted", enlisted)
				.toString();
	}

	public void close() {
		if (!closed) {
			try {
				if (!enlisted) {
					connection.close();
				}
			} finally {
				closed = true;
			}

		}
	}

	public int commit() {
		if (enlisted) {
			throw new IllegalStateException(
					NLS.MESSAGES.getString("connection.enlisted.commit"));
		}
		return connection.commit();
	}

	public int connectionName(AMString connectionName) {
		return connection.connectionName(connectionName);
	}

	public int convertDateBasicToUnix(int tmTime) {
		return connection.convertDateBasicToUnix(tmTime);
	}

	public int convertDateIntlToUnix(String dateAsString) {
		return connection.convertDateIntlToUnix(dateAsString);
	}

	public int convertDateStringToUnix(String dateAsString) {
		return connection.convertDateStringToUnix(dateAsString);
	}

	public int convertDateUnixToBasic(int dateAsUnix) {
		return connection.convertDateUnixToBasic(dateAsUnix);
	}

	public int convertDateUnixToIntl(int UnixDate, AMString dateAsIntlStr) {
		return connection.convertDateUnixToIntl(UnixDate, dateAsIntlStr);
	}

	public int convertDateUnixToString(int dateAsUnix, AMString dateAsStr) {
		return connection.convertDateUnixToString(dateAsUnix, dateAsStr);
	}

	public int convertDoubleToString(double dSrc, AMString dblAsString) {
		return connection.convertDoubleToString(dSrc, dblAsString);
	}

	public int convertMonetaryToString(double dMonetarySrc, AMString dblAsString) {
		return connection.convertMonetaryToString(dMonetarySrc, dblAsString);
	}

	public double convertStringToDouble(String dblAsString) {
		return connection.convertStringToDouble(dblAsString);
	}

	public double convertStringToMonetary(String monetaryAsString) {
		return connection.convertStringToMonetary(monetaryAsString);
	}

	public int createLink(int srcRecHandle, String linkName, int srcDstHandle) {
		return connection.createLink(srcRecHandle, linkName, srcDstHandle);
	}

	public int createRecord(String tblName) {
		return connection.createRecord(tblName);
	}

	public int currentDate() {
		return connection.currentDate();
	}

	public int currentServerDate() {
		return connection.currentServerDate();
	}

	public int dateAdd(int startAsUnixDate, int duration) {
		return connection.dateAdd(startAsUnixDate, duration);
	}

	public int dateAddLogical(int startAsUnixDate, int duration) {
		return connection.dateAddLogical(startAsUnixDate, duration);
	}

	public int dateDiff(int endAsUnixDate, int startAsUnixDate) {
		return connection.dateDiff(endAsUnixDate, startAsUnixDate);
	}

	public int dbExecAql(String aqlQuery) {
		return connection.dbExecAql(aqlQuery);
	}

	public int dbGetDate(String aqlQuery) {
		return connection.dbGetDate(aqlQuery);
	}

	public double dbGetDouble(String aqlQuery) {
		return connection.dbGetDouble(aqlQuery);
	}

	public int dbGetLimitedList(String aqlQuery, AMString result,
			String colSeparator, String lineSeparator, String idSeperator,
			int maxSize, int errorType) {
		return connection.dbGetLimitedList(aqlQuery, result, colSeparator,
				lineSeparator, idSeperator, maxSize, errorType);
	}

	public int dbGetList(String aqlQuery, AMString result, String colSeparator,
			String lineSeparator, String idSeperator) {
		return connection.dbGetList(aqlQuery, result, colSeparator,
				lineSeparator, idSeperator);
	}

	public int dbGetListEx(String aqlQuery, AMString result,
			String colSeparator, String lineSeparator, String idSeperator) {
		return connection.dbGetListEx(aqlQuery, result, colSeparator,
				lineSeparator, idSeperator);
	}

	public int dbGetLong(String aqlQuery) {
		return connection.dbGetLong(aqlQuery);
	}

	public int dbGetPk(String tableName, String whereClause) {
		return connection.dbGetPk(tableName, whereClause);
	}

	public int dbGetString(String query, AMString result, String colSeparator,
			String lineSeparator) {
		return connection.dbGetString(query, result, colSeparator,
				lineSeparator);
	}

	public int dbGetStringEx(String query, AMString result,
			String colSeparator, String lineSeparator) {
		return connection.dbGetStringEx(query, result, colSeparator,
				lineSeparator);
	}

	public int deleteLink(int srcRecHandle, String linkName, int srcDstHandle) {
		return connection.deleteLink(srcRecHandle, linkName, srcDstHandle);
	}

	public int deleteRecord(int recordHandle) {
		return connection.deleteRecord(recordHandle);
	}

	public int duplicateRecord(int recordHandle, int insert) {
		return connection.duplicateRecord(recordHandle, insert);
	}

	public int enumValList(String enumName, AMString value, int caseSensitive,
			String lineSeparator) {
		return connection.enumValList(enumName, value, caseSensitive,
				lineSeparator);
	}

	public int executeActionById(int actionId, String tableName, int recordId) {
		return connection.executeActionById(actionId, tableName, recordId);
	}

	public int executeActionByName(String sqlName, String tableName,
			int recordId) {
		return connection.executeActionByName(sqlName, tableName, recordId);
	}

	public int exportDocument(int documentId, String fileName) {
		return connection.exportDocument(documentId, fileName);
	}

	public int flushTransaction() {
		return connection.flushTransaction();
	}

	public int formatCurrency(double amount, String currency, AMString result) {
		return connection.formatCurrency(amount, currency, result);
	}

	public int formatLong(int number, String format, AMString result) {
		return connection.formatLong(number, format, result);
	}

	public int getComputeString(String tableName, int recordId,
			String template, AMString result) {
		return connection.getComputeString(tableName, recordId, template,
				result);
	}

	public int getField(int objHandle, int position) {
		return connection.getField(objHandle, position);
	}

	public int getFieldCount(int objHandle) {
		return connection.getFieldCount(objHandle);
	}

	public int getFieldDateOnlyValue(int recHandle, int fieldPos) {
		return connection.getFieldDateOnlyValue(recHandle, fieldPos);
	}

	public int getFieldDateValue(int recHandle, int fieldPos) {
		return connection.getFieldDateValue(recHandle, fieldPos);
	}

	public int getFieldDescription(int fieldHandle, AMString target) {
		return connection.getFieldDescription(fieldHandle, target);
	}

	public double getFieldDoubleValue(int objHandle, int fieldPos) {
		return connection.getFieldDoubleValue(objHandle, fieldPos);
	}

	public int getFieldFormat(int fldHandle, AMString target) {
		return connection.getFieldFormat(fldHandle, target);
	}

	public int getFieldFormatFromName(String tableName, String fieldName,
			AMString result) {
		return connection.getFieldFormatFromName(tableName, fieldName, result);
	}

	public int getFieldFromName(int objHandle, String fielddName) {
		return connection.getFieldFromName(objHandle, fielddName);
	}

	public int getFieldLabel(int fldHandle, AMString result) {
		return connection.getFieldLabel(fldHandle, result);
	}

	public int getFieldLabelFromName(String tableName, String fieldName,
			AMString fieldLabel) {
		return connection.getFieldLabelFromName(tableName, fieldName,
				fieldLabel);
	}

	public int getFieldLongValue(int objHandle, int fieldPosition) {
		return connection.getFieldLongValue(objHandle, fieldPosition);
	}

	public int getFieldName(int objHandle, int fieldPositon, AMString fieldName) {
		return connection.getFieldName(objHandle, fieldPositon, fieldName);
	}

	public int getFieldSize(int fldHandle) {
		return connection.getFieldSize(fldHandle);
	}

	public int getFieldSqlName(int fldHandle, AMString fieldSQLName) {
		return connection.getFieldSqlName(fldHandle, fieldSQLName);
	}

	public int getFieldStrValue(int qryHandle, int position, AMString target) {
		return connection.getFieldStrValue(qryHandle, position, target);
	}

	public int getFieldType(int fldHandle) {
		return connection.getFieldType(fldHandle);
	}

	public int getFieldUserType(int fldHandle) {
		return connection.getFieldUserType(fldHandle);
	}

	public int getRecordFromMainId(String tableName, int lId) {
		return connection.getRecordFromMainId(tableName, lId);
	}

	public int getRecordHandle(int qryHandle) {
		return connection.getRecordHandle(qryHandle);
	}

	public int getRecordId(int recHandle) {
		return connection.getRecordId(recHandle);
	}

	public int getRelDstField(int fldHandle) {
		return connection.getRelDstField(fldHandle);
	}

	public int getRelSrcField(int fldHandle) {
		return connection.getRelSrcField(fldHandle);
	}

	public int getRelTable(int fldHandle) {
		return connection.getRelTable(fldHandle);
	}

	public int getReverseLink(int fldHandle) {
		return connection.getReverseLink(fldHandle);
	}

	public int getSelfFromMainId(String tableName, int recordId,
			AMString recordDescription) {
		return connection.getSelfFromMainId(tableName, recordId,
				recordDescription);
	}

	public int getVersion(AMString amVersion) {
		return connection.getVersion(amVersion);
	}

	public int importDocument(int docId, String tableName, String fileName,
			String category, String designation) {
		return connection.importDocument(docId, tableName, fileName, category,
				designation);
	}

	public int insertRecord(int recHandle) {
		return connection.insertRecord(recHandle);
	}

	public int isConnected() {
		return connection.isConnected();
	}

	public int lastError() {
		return connection.lastError();
	}

	public int lastErrorMsg(AMString errorMessage) {
		return connection.lastErrorMsg(errorMessage);
	}

	public int listToString(AMString target, String source, String colSep,
			String lineSep, String idSep) {
		return connection.listToString(target, source, colSep, lineSep, idSep);
	}

	public int loginId() {
		return connection.loginId();
	}

	public int loginName(AMString loginName) {
		return connection.loginName(loginName);
	}

	public int openConnection(String database, String username, String password) {
		return connection.openConnection(database, username, password);
	}

	public int purgeRecord(int recHandle) {
		return connection.purgeRecord(recHandle);
	}

	public int queryCreate() {
		return connection.queryCreate();
	}

	public int queryExec(int queryHandle, String aqlQuery) {
		return connection.queryExec(queryHandle, aqlQuery);
	}

	public int queryGet(int qryHandle, String aqlQuery) {
		return connection.queryGet(qryHandle, aqlQuery);
	}

	public int queryNext(int queryHandle) {
		return connection.queryNext(queryHandle);
	}

	public int querySetAddMainField(int qryHandle, int addMainField) {
		return connection.querySetAddMainField(qryHandle, addMainField);
	}

	public int querySetFullMemo(int qryHandle, int fullMemo) {
		return connection.querySetFullMemo(qryHandle, fullMemo);
	}

	public int queryStartTable(int qryHandle) {
		return connection.queryStartTable(qryHandle);
	}

	public int queryStop(int qryHandle) {
		return connection.queryStop(qryHandle);
	}

	public int refreshAllCaches() {
		return connection.refreshAllCaches();
	}

	public int releaseHandle(int objHandle) {
		return connection.releaseHandle(objHandle);
	}

	public int rollBack() {
		if (enlisted) {
			throw new IllegalStateException(
					NLS.MESSAGES.getString("connection.enlisted.rollback"));
		}
		return connection.rollBack();
	}

	public int setFieldDateOnlyValue(int recHandle, String fieldName,
			AMDate dateOnlyValue) {
		return connection.setFieldDateOnlyValue(recHandle, fieldName,
				dateOnlyValue);
	}

	public int setFieldDateValue(int recHandle, String fieldName,
			AMDate dateTimeValue) {
		return connection
				.setFieldDateValue(recHandle, fieldName, dateTimeValue);
	}

	public int setFieldDoubleValue(int recHandle, String fieldName, double value) {
		return connection.setFieldDoubleValue(recHandle, fieldName, value);
	}

	public int setFieldLongValue(int recHandle, String fieldName, int value) {
		return connection.setFieldLongValue(recHandle, fieldName, value);
	}

	public int setFieldStrValue(int recHandle, String fieldName, String value) {
		return connection.setFieldStrValue(recHandle, fieldName, value);
	}

	public int sqlTextConst(AMString target, String aqlQuery) {
		return connection.sqlTextConst(target, aqlQuery);
	}

	public int startTransaction() {
		return connection.startTransaction();
	}

	public int updateRecord(int recHandle) {
		return connection.updateRecord(recHandle);
	}

}
